package com.demo.controller;

import com.demo.pojo.Inout;
import com.demo.pojo.User;
import com.demo.service.InoutService;
import com.demo.utils.DateUtils;
import com.demo.utils.Page;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author： Administrator
 * @date: 21/05/17 09:53
 * 描述：
 */
@Controller
@RequestMapping("/inout")
public class InoutController {
    @Autowired
    private InoutService inoutService;

    @RequestMapping("/page.do")
    @ResponseBody
    public Map<String, Object> page(
            String content,
            @RequestParam(required = false) String back,
            @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "0") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("message", "");
        content = "%" + content + "%";
        Page<Inout> page = inoutService.page(content, back, pageNo, pageSize);
        map.put("count", page.getPageTotalCount());
        map.put("data", page.getItems());
        return map;
    }

    @PostMapping("/out.do")
    @ResponseBody
    public Map<String, Object> out(Inout inout) {
        Map<String, Object> map = new HashMap<>();
        int out;
        try {
            out = inoutService.out(inout);
        } catch (Exception e) {
            map.put("code", -1);
            map.put("message", "服务器发生了异常！");
            return map;
        }
        switch (out) {
            case -4:
                map.put("code", 5);
                map.put("message", "借出失败！原因：修改资产状态失败！");
                return map;
            case -3:
                map.put("code", 4);
                map.put("message", "借出失败！原因：借出人编号错误！");
                return map;
            case -2:
                map.put("code", 3);
                map.put("message", "借出失败！原因：资产可能借出/维修/报销！");
                return map;
            case -1:
                map.put("code", 2);
                map.put("message", "借出失败！原因：资产不存在，可能已被销毁！");
                return map;
            case 0:
                map.put("code", 1);
                map.put("message", "借出失败！");
                return map;
            case 1:
                map.put("code", 0);
                map.put("message", "借出成功！");
                return map;
            default:
                map.put("code", -1);
                map.put("message", "未知错误！");
                return map;
        }
    }

    @PostMapping("/back.do")
    @ResponseBody
    public Map<String, Object> back(
            @RequestParam(name = "no", required = false) Integer inoutNo) {
        Map<String, Object> map = new HashMap<>();
        Inout inout = new Inout();
        inout.setInoutNo(inoutNo);
        int back = inoutService.back(inout);
        switch (back) {
            case -4:
                map.put("code", 5);
                map.put("message", "归还失败，原因：修改资产状态失败！");
                return map;
            case -3:
                map.put("code", 4);
                map.put("message", "归还失败，原因：资产已归还，无需归还！");
                return map;
            case -2:
                map.put("code", 3);
                map.put("message", "归还失败，原因：资产已归还，无需归还！");
                return map;
            case -1:
                map.put("code", 2);
                map.put("message", "归还失败，原因：该条记录不存在，请刷新页面！");
                return map;
            case 0:
                map.put("code", 1);
                map.put("message", "归还失败！");
                return map;
            case 1:
                map.put("code", 0);
                map.put("message", "归还成功！");
                return map;
            default:
                map.put("code", -1);
                map.put("message", "未知错误！");
                return map;
        }
    }
}
